package com.feng.newconfig.config.feign;

import feign.Target;
import lombok.AllArgsConstructor;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cloud.openfeign.FallbackFactory;

/**
 * @ClassName FunFallbackFactory
 * @Description
 * @Author Tyler
 * @Date 2020/7/1 16:47
 **/
@AllArgsConstructor
public class FunFallbackFactory<T> implements FallbackFactory<T> {

    private final Target<T> target;

    @Override
    public T create(Throwable cause) {
        final Class<T> targetType = target.type();
        final String targetName = target.name();
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(targetType);
        enhancer.setUseCache(true);
        enhancer.setCallback(new FunFeignFallback<>(targetType, targetName, cause));
        return (T) enhancer.create();
    }
}
